package com.fenghongzhang.day008;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class SDActivity extends AppCompatActivity {
    private Button sdWrite;
    private Button sdRead;

    private static final String TAG = "SDActivity";


    /**
     * 请求权限的回调方法
     * @param requestCode  请求码
     * @param permissions  所有请求权限
     * @param grantResults 用户点击了同意还是不同意.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //如果请求码是100 并且用户点击了同意
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "同意", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "不同意", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_d);

        sdWrite = (Button) findViewById(R.id.sd_write);
        sdRead = (Button) findViewById(R.id.sd_read);


        /**
         * 从6.0 开始,有一些权限,需要动态请求,才可以正常使用.
         *
         * 1,正常权限 -> 清单文件
         *
         * 2,危险权限 -> 清单文件,代码中,动态请求.
         *
         */

        //1,版本判断
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //2,动态请求
            //2.1 权限数据 2.2请求码
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE
                    ,Manifest.permission.WRITE_EXTERNAL_STORAGE} , 100);

        }


        String externalStorageState = Environment.getExternalStorageState();
        Log.i(TAG, "onCreate: "+externalStorageState);
        File externalStorageDirectory = Environment.getExternalStorageDirectory();
        Log.i(TAG, "onCreate: "+externalStorageDirectory);

        sdWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //写
                //先判断sd 挂载 mounted
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    //取到路径,然后写入
                    File file = Environment.getExternalStorageDirectory();

                    //打开输出流 ///storage/emulated/0/one.txt
                        FileOutputStream fileOutputStream = null;
                    try {
                        fileOutputStream = new FileOutputStream(new File(file, "one.txt"));
                        fileOutputStream.write("diyige".getBytes());
                        fileOutputStream.flush();
                        fileOutputStream.close();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        Log.i(TAG, "onClick: "+e);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }


            }
        });


    }
}
